package org.lee.img;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * ImageIconTabbedPaneDemo
 * JTabbedPane中ImageIcon
 *
 * @author dralee
 * @date 2021-12-29 17:35:07
 **/
public class ImageIconTabbedPaneDemo extends JFrame {
    public ImageIconTabbedPaneDemo() throws HeadlessException {
        initUI();
    }

    private URL resource(String name) {
        return getClass().getClassLoader().getResource("images/".concat(name).concat(".jpg"));
    }

    private void initUI() {
        ImageIcon icon1 = new ImageIcon(resource("dot1"));
        ImageIcon icon2 = new ImageIcon(resource("dot2"));
        ImageIcon icon3 = new ImageIcon(resource("dot3"));

        JTabbedPane tp = new JTabbedPane();
        tp.setPreferredSize(new Dimension(350, 250));

        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("这是第一页"));
        JPanel panel2 = new JPanel();
        panel2.add(new JLabel("这是第二页"));
        JPanel panel3 = new JPanel();
        panel3.add(new JLabel("这是第三页"));


        tp.addTab("", icon1, panel1);
        tp.addTab("", icon2, panel2);
        tp.addTab("", icon3, panel3);

        createLayout(tp);

        setTitle("Icons");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setIconImage(new ImageIcon(resource("android-32")).getImage());
    }

    private void createLayout(JComponent... arg) {
        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);
        gl.setHorizontalGroup(gl.createSequentialGroup().addComponent(arg[0]));
        gl.setVerticalGroup(gl.createParallelGroup().addComponent(arg[0]));

        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new ImageIconTabbedPaneDemo().setVisible(true));
    }
}
